package com.example.demo;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.noear.solon.scheduling.annotation.Async;

@Slf4j
@Component
public class TaskServiceImpl implements TaskService {

    @Async
    @Override
    public void asyncTest(String jsonStr) {
        log.info("start task---------------------------");
        JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
        String requestId = jsonObject.getStr("request_id");
        Long sleepSeconds = jsonObject.get("sleep_seconds", Long.class);
        String callbackUrl = jsonObject.getStr("callback_url");
        try {
            Thread.sleep(1000 * sleepSeconds);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        JSONObject httpBodyJson = new JSONObject();
        httpBodyJson.set("request_id", requestId);
        httpBodyJson.set("test", "test");
        String s = HttpUtil.post(callbackUrl, httpBodyJson.toJSONString(0));
        log.info("s={}", s);
    }
}
